package com.jinkaimen.goodcash.ui.activity;

import android.app.ProgressDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ht.baselibs.bean.ApkInfoBean;
import com.ht.baselibs.constant.SPConfig;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.DeviceUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.ht.baselibs.utils.UpdateDialogUtils;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.factory.FragmentFactory;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.utils.AppUtils;

import retrofit2.Call;
import retrofit2.Response;

public class HomeActivity extends BaseActivity {
    private long exitTime;
    private TextView mHomeTabView;
    private TextView mSelfTabView;
    private ProgressDialog mUpdateDialog;
    private boolean isForcedUpdate;

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("好收银").setLeftBtnVisibility(View.GONE);//
//                .setLeftBtnIcon(R.mipmap.msg_home_frag);//
//                .addActions(new TitleBarView.Action(R.mipmap.help_home_frag) {
//                    @Override
//                    public void performAction(View view) {
//
//                    }
//                });
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_home;
    }

    @Override
    protected void initChildView() {
        mHomeTabView = (TextView) findViewById(R.id.rb_home_tab_home);
        mSelfTabView = (TextView) findViewById(R.id.rb_home_tab_self);

        mHomeTabView.setSelected(true);
        switchFragment(FragmentFactory.getHomeTabFragment(FragmentFactory.FRAGMENT_HOME_TAB0), R.id.fl_activity_home_content);
    }

    @Override
    protected void initChildData() {
        ApiManager.getApi().getUpdateApkUrl(
                "HSY001027"
        ).enqueue(new NetCallBack<ApkInfoBean>() {
            @Override
            protected void onSuccess(Call<ApkInfoBean> call, Response<ApkInfoBean> response) {
                ApkInfoBean apkInfoBean = response.body();
                if (apkInfoBean.isUpgrade != 0 && DeviceUtils.getAppVersionCode() < apkInfoBean.versionCode) {
                    String newVersionName = apkInfoBean.versionName;
                    String ignoreVersionName = SharedPreferencesUtils.getString(SPConfig.IGNORE_APP_UPDATE_VERSION, "");
                    if ("".equals(ignoreVersionName) || !ignoreVersionName.equals(newVersionName)) {

                        String[] versionStr = newVersionName.split("\\.");

                        isForcedUpdate = versionStr.length >= UpdateDialogUtils.FORCED_UPDATE_LENG;

                        mUpdateDialog = new ProgressDialog(HomeActivity.this);
                        mUpdateDialog.setTitle("下载最新安装包");
                        mUpdateDialog.setMessage("正在下载，请稍后...");
                        mUpdateDialog.setMax(100);
                        mUpdateDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

//                        apkInfoBean.versionUrl = "http://192.168.1.37:8080/app1.0.1.apk";

                        UpdateDialogUtils.showUpdateDialog(HomeActivity.this, apkInfoBean, mUpdateDialog);
                    }
                }
            }


        });
    }

    @Override
    protected void setChildViewListener() {
        mHomeTabView.setOnClickListener(this);
        mSelfTabView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        BaseFragment baseFragment = null;
        switch (v.getId()){
            case R.id.rb_home_tab_home:
                resetTab();
                mHomeTabView.setSelected(true);

                mTitleBarView.setTitleText("好收银");//
//                        .setLeftBtnIcon(R.mipmap.msg_home_frag)//
//                        .setLeftBtnVisibility(View.VISIBLE)
//                        .removeAllAction()
//                        .addActions(new TitleBarView.Action(R.mipmap.help_home_frag) {
//                            @Override
//                            public void performAction(View view) {
//
//                            }
//                        });
                baseFragment = FragmentFactory.getHomeTabFragment(FragmentFactory.FRAGMENT_HOME_TAB0);
                switchFragment(baseFragment, R.id.fl_activity_home_content);
                break;
            case R.id.rb_home_tab_self:
                if (!AppUtils.isLogin()){
                    ActivityUtils.startActivity(HomeActivity.this, LoginActivity.class);
                    return;
                }

                resetTab();
                mSelfTabView.setSelected(true);

                mTitleBarView.setTitleText("我的");//
//                        .setLeftBtnVisibility(View.GONE)//
//                        .removeAction(0);
                baseFragment = FragmentFactory.getHomeTabFragment(FragmentFactory.FRAGMENT_HOME_TAB1);
                switchFragment(baseFragment, R.id.fl_activity_home_content);
                break;
        }
    }

    @Override
    protected void onEvent(int code, Object data) {
        switch (code){
            case EventCode.EVENT_CODE_START_LOGOUT:
                resetTab();
                mHomeTabView.setSelected(true);
                mTitleBarView.setTitleText("好收银");//
//                        .setLeftBtnIcon(R.mipmap.msg_home_frag)//
//                        .setLeftBtnVisibility(View.VISIBLE)
//                        .removeAllAction()
//                        .addActions(new TitleBarView.Action(R.mipmap.help_home_frag) {
//                            @Override
//                            public void performAction(View view) {
//
//                            }
//                        });
                BaseFragment baseFragment = FragmentFactory.getHomeTabFragment(FragmentFactory.FRAGMENT_HOME_TAB0);
                switchFragment(baseFragment, R.id.fl_activity_home_content);
                break;
            case EventCode.EVENT_CODE_UPDATE_APK_PROGRESS:
                mUpdateDialog.setProgress((Integer) data);
                break;
            case EventCode.EVENT_CODE_UPDATE_APK_END:
                DeviceUtils.installApk(this, (String)data);
                if (!isForcedUpdate){
                    mUpdateDialog.dismiss();
                }
                break;
            case EventCode.EVENT_CODE_UPDATE_APK_ERROR:
                UIUtils.showToast("您当前的网络不稳定，下载安装包失败");
                mUpdateDialog.dismiss();
                break;
        }
    }

    private void resetTab() {
        mHomeTabView.setSelected(false);
        mSelfTabView.setSelected(false);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            checkFinishApp(event);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    private void checkFinishApp(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                UIUtils.showToast("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                ActivityUtils.finishActivityByAlpha(this);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FragmentFactory.mHomeTabMap.clear();
    }
}
